/*
 * Copyright (C) 2018 Zhejiang xiaominfo Technology CO.,LTD.
 * All rights reserved.
 * Official Web Site: http://www.xiaominfo.com.
 * Developer Web Site: http://open.xiaominfo.com.
 */

package com.github.xiaoymin.map.adapter;

import com.github.xiaoymin.map.response.amap.regeo.AmapRegeoAddressComponent;
import com.google.gson.*;

import java.lang.reflect.Type;

/**
 * @author <a href="mailto:xiaoymin@foxmail.com">xiaoymin@foxmail.com</a>
 * 2020/09/23 9:47
 * @since:map-client 1.0
 */
public class AmapAddressAdapter implements JsonDeserializer<AmapRegeoAddressComponent> {


    @Override
    public AmapRegeoAddressComponent deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
        JsonElement city=json.getAsJsonObject().get("city");
        if (city!=null&&city.isJsonArray()){
            System.out.println("city为数组");
            JsonObject copyJson=json.deepCopy().getAsJsonObject();
            copyJson.addProperty("city","");
            return new Gson().fromJson(copyJson,AmapRegeoAddressComponent.class);
        }else{
            return new Gson().fromJson(json,AmapRegeoAddressComponent.class);
        }
    }
}
